// 业务介绍页面的JavaScript

// 等待DOM加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 处理导航栏滚动效果
    window.addEventListener('scroll', function() {
        const navbar = document.querySelector('.navbar');
        if (window.scrollY > 50) {
            navbar.classList.add('bg-white', 'shadow-md');
            navbar.classList.remove('bg-transparent');
        } else {
            navbar.classList.add('bg-white');
            navbar.classList.remove('shadow-md', 'bg-transparent');
        }
    });

    // 处理关于我们的弹窗交互
    const aboutUsBtn = document.getElementById('aboutUsBtn');
    const aboutUsDropdown = document.getElementById('aboutUsDropdown');

    if (aboutUsBtn && aboutUsDropdown) {
        aboutUsBtn.addEventListener('click', function(e) {
            e.preventDefault();
            aboutUsDropdown.classList.toggle('show');
        });
        
        // 点击其他区域关闭下拉菜单
        document.addEventListener('click', function(e) {
            if (aboutUsBtn && aboutUsDropdown && !aboutUsBtn.contains(e.target) && !aboutUsDropdown.contains(e.target)) {
                aboutUsDropdown.classList.remove('show');
            }
        });
        
        // 为下拉菜单中的荣誉证书和合作伙伴按钮添加点击事件
        const dropdownItems = document.querySelectorAll('#aboutUsDropdown .dropdown-item');
        dropdownItems.forEach(item => {
            item.addEventListener('click', function() {
                // 确保点击后能正常跳转到相应页面
                if (this.textContent.trim() === '合作伙伴') {
                    this.setAttribute('href', 'certificates.html?type=partners');
                } else if (this.textContent.trim() === '荣誉证书') {
                    this.setAttribute('href', 'certificates.html?type=certificates');
                }
            });
        });
    }
    


    // 确保弹窗在移动端可以正常显示
    const aboutUsContainer = document.getElementById('aboutUsContainer');
    const isMobile = window.innerWidth <= 768;
    if (aboutUsContainer && isMobile) {
        aboutUsContainer.addEventListener('click', function(e) {
            // 只有在点击'关于我们'文本时才触发下拉菜单，避免影响链接跳转
            if (e.target === aboutUsContainer || e.target === aboutUsContainer.querySelector('.nav-link')) {
                e.preventDefault();
                if (aboutUsDropdown) {
                    aboutUsDropdown.classList.toggle('show');
                }
            }
        });
    }

    // 业务项目悬停效果增强
    const businessItems = document.querySelectorAll('.business-item');
    businessItems.forEach(item => {
        item.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-10px)';
            this.style.boxShadow = '0 15px 30px rgba(0, 0, 0, 0.15)';
        });
        
        item.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(-5px)';
            this.style.boxShadow = '0 10px 25px rgba(0, 0, 0, 0.1)';
        });
    });

    // 为统计数字添加计数动画
    const animateNumbers = function() {
        const achievementNumbers = document.querySelectorAll('.achievement-number');
        
        achievementNumbers.forEach(number => {
            // 提取数字，排除'+'字符
            const textContent = number.textContent;
            const target = parseInt(textContent.replace(/\D/g, ''));
            let count = 0;
            const duration = 2000; // 动画持续时间
            const increment = target / (duration / 16);
            
            const updateCount = function() {
                count += increment;
                if (count < target) {
                    number.textContent = Math.floor(count) + (textContent.includes('+') ? '+' : '');
                    requestAnimationFrame(updateCount);
                } else {
                    number.textContent = target + (textContent.includes('+') ? '+' : '');
                }
            };
            
            updateCount();
        });
    };

    // 检测元素是否在视口中
    const isInViewport = function(element) {
        const rect = element.getBoundingClientRect();
        return (
            rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
            rect.bottom >= 0
        );
    };

    // 当滚动到统计区域时触发计数动画
    let animated = false;
    window.addEventListener('scroll', function() {
        const statisticsSection = document.getElementById('statistics');
        if (statisticsSection && isInViewport(statisticsSection) && !animated) {
            animateNumbers();
            animated = true;
        }
    });

    // 优势项目悬停效果增强
    const advantageItems = document.querySelectorAll('.advantage-item');
    advantageItems.forEach(item => {
        item.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-10px)';
            this.style.boxShadow = '0 15px 30px rgba(0, 0, 0, 0.15)';
        });
        
        item.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(-5px)';
            this.style.boxShadow = '0 10px 25px rgba(0, 0, 0, 0.1)';
        });
    });

    // 移动端导航菜单关闭处理
    document.querySelectorAll('.nav-link, .dropdown-item').forEach(link => {
        link.addEventListener('click', function() {
            const navbarCollapse = document.getElementById('navbarNav');
            if (navbarCollapse && navbarCollapse.classList.contains('show')) {
                const navbarToggler = document.querySelector('.navbar-toggler');
                if (navbarToggler) {
                    navbarToggler.click();
                }
            }
        });
    });

    // 服务流程动画效果
    const processSteps = document.querySelectorAll('.process-step');
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.style.opacity = '1';
                entry.target.style.transform = 'translateY(0)';
                observer.unobserve(entry.target);
            }
        });
    }, {
        threshold: 0.1
    });

    processSteps.forEach(step => {
        step.style.opacity = '0';
        step.style.transform = 'translateY(20px)';
        step.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
        observer.observe(step);
    });
});